package com.tobias.gaokao.service.impl;

import com.tobias.gaokao.constants.PTConstants;
import com.tobias.gaokao.constants.Result;
import com.tobias.gaokao.constants.ResultEnum;
import com.tobias.gaokao.constants.ResultUtil;
import com.tobias.gaokao.dao.ArtScoreRepository;
import com.tobias.gaokao.dao.SciScoreRepository;
import com.tobias.gaokao.model.ArtScore;
import com.tobias.gaokao.model.SciScore;
import com.tobias.gaokao.model.dto.DisplayDto;
import com.tobias.gaokao.service.ScoreService;
import org.apache.commons.lang.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
/**
 * 
 * @author TobiasWang 2020/11/17
 */
@Service
public class ScoreServiceImpl implements ScoreService {
	private final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

	@Autowired
	public SciScoreRepository sciScoreRepository;

	@Autowired
	public ArtScoreRepository artScoreRepository;

	@Override
	public Result getScore(String uniqueNumber, HttpSession session) {

		int studentType = (int) session.getAttribute(PTConstants.STUDENT_TYPE);
		DisplayDto dto = new DisplayDto();
		dto.setName((String) session.getAttribute(PTConstants.USER_NAME));
		dto.setUniqueNumber(uniqueNumber);
		if(1 == studentType) {
			SciScore sciScore = sciScoreRepository.findByUniqueNumber(uniqueNumber);
			if (null == sciScore) {
				return ResultUtil.error(ResultEnum.WRONG_PARAM);
			}
			dto.setChinese(String.valueOf(sciScore.getChinese()));
			dto.setMathematics(String.valueOf(sciScore.getMathematics()));
			dto.setEnglish(String.valueOf(sciScore.getEnglish()));
			dto.setComprehensiveTitle("理科综合");
			dto.setComprehensive(String.valueOf(sciScore.getSciComprehensive()));
			dto.setTotalScore(String.valueOf(sciScore.getTotalScore()));
			dto.setOrder(String.valueOf(sciScore.getOrder()));
		}
		if(0 == studentType) {
			ArtScore artScore = artScoreRepository.findByUniqueNumber(uniqueNumber);
			if (null == artScore) {
				return ResultUtil.error(ResultEnum.WRONG_PARAM);
			}
			dto.setChinese(String.valueOf(artScore.getChinese()));
			dto.setMathematics(String.valueOf(artScore.getMathematics()));
			dto.setEnglish(String.valueOf(artScore.getEnglish()));
			dto.setComprehensiveTitle("文科综合");
			dto.setComprehensive(String.valueOf(artScore.getArtComprehensive()));
			dto.setTotalScore(String.valueOf(artScore.getTotalScore()));
			dto.setOrder(String.valueOf(artScore.getOrder()));
		}
		return ResultUtil.OTSResult(dto, dto.getName()+ PTConstants.STATEMENT_DETAILS);
	}
//
//	@Resource
//	private StatementDao statementMapper;
//
//	@Resource
//	private UserDao userMapper;
//
//	public Result getStatementTableList(String input, HttpSession session) {
//		JSONObject jsonObj = JSONObject.fromObject(input);
//		int accountId = Integer.parseInt(jsonObj.getString("accountId"));
//		String searchStartDate = jsonObj.getString("searchStartDate");
//		String searchEndDate = jsonObj.getString("searchEndDate");
//		String searchStartAmount = jsonObj.getString("searchStartAmount");
//		String searchEndAmount = jsonObj.getString("searchEndAmount");
//
//		if (!StringUtils.isEmpty(searchStartDate)) {
//			try {
//				searchStartDate = simpleDateFormatDB.format(simpleDateFormatUI.parse(searchStartDate));
//			} catch (ParseException e) {
//				e.printStackTrace();
//				logger.error("->the date can not be parsed, the detail is searchStartDate:{}", searchStartDate);
//			}
//		}
//		if (!StringUtils.isEmpty(searchEndDate)) {
//			try {
//				searchEndDate = simpleDateFormatDB.format(simpleDateFormatUI.parse(searchEndDate));
//			} catch (ParseException e) {
//				e.printStackTrace();
//				logger.error("->the date can not be parsed, the detail is searchEndDate:{}", searchEndDate);
//			}
//		}
//
//		SearchDto searchDto = new SearchDto(accountId, searchStartDate, searchEndDate, searchStartAmount, searchEndAmount);
//
//		List<SysStatement> allStatements = SysStatementUtil.convert(statementMapper.selectAllStatements())
//				.stream().filter(s-> s.getAccountId()==searchDto.getAccountId()).collect(Collectors.toList());
//
//		if (allParamEmpty(searchDto)) {
//			logger.error("-> input all empty => will return default result for 3 months");
//			List<SysStatement> list = allStatements.stream()
//					.filter(s -> SysStatementUtil.withinLastThreeMonth(s.getDateField()) == true)
//					.collect(Collectors.toList());
//			return ResultUtil.OTSResult(findDisplayDto(list), PTConstants.THREE_MONTH_DETAILS);
//		}
//
//		if (!((int) session.getAttribute(PTConstants.PERMISSION_LEVEL) == PTConstants.ADMIN)) {
//			return ResultUtil.error(ResultEnum.PERMISSION_DENNY);
//		}
//
//		if (!isValid(searchDto)) {
//			logger.error("->search wrong parameters error, the detail is：input:{}", searchDto);
//			return ResultUtil.error(ResultEnum.WRONG_PARAM);
//		}
//		return conditionalSearch(allStatements, searchDto);
//	}
//
//	private List<DisplayDto> findDisplayDto(List<SysStatement> statements) {
//		List<DisplayDto> dtos = new ArrayList<DisplayDto>();
//		statements.forEach(statement -> {
//			DisplayDto dto = new DisplayDto();
//			dto.setAccountId(statement.getAccountId());
//			dto.setAccountNumber(CommonUtils.toHash(userMapper.findAccountById(statement.getAccountId())));
//			dto.setDateField(statement.getDateField().toString());
//			dto.setAmount("" + statement.getAmount());
//			dtos.add(dto);
//		});
//		return dtos;
//	}
//
//	private boolean allParamEmpty(SearchDto dto) {
//		return StringUtils.isEmpty(dto.getSearchStartDate()) && StringUtils.isEmpty(dto.getSearchEndDate())
//				&& StringUtils.isEmpty(dto.getSearchStartAmount()) && StringUtils.isEmpty(dto.getSearchEndAmount());
//	}
//
//	private boolean isValid(SearchDto searchDto) {
//		return SysStatementUtil.isDateFormat(searchDto.getSearchStartDate())
//				&& SysStatementUtil.isDateFormat(searchDto.getSearchEndDate())
//				&& SysStatementUtil.isAmount(searchDto.getSearchStartAmount())
//				&& SysStatementUtil.isAmount(searchDto.getSearchEndAmount());
//	}
//
//	private Result conditionalSearch(List<SysStatement> allStatements, SearchDto dto) {
//		List<SysStatement> list = allStatements;
//		if (!StringUtils.isEmpty(dto.getSearchStartDate())) {
//			list = list.stream()
//					.filter(s -> s.getDateField()
//							.isAfter(SysStatementUtil.changeToDate(dto.getSearchStartDate()).minus(1, ChronoUnit.DAYS)))
//					.collect(Collectors.toList());
//		}
//		if ((!StringUtils.isEmpty(dto.getSearchEndDate()))) {
//			list = list.stream()
//					.filter(s -> s.getDateField()
//							.isBefore(SysStatementUtil.changeToDate(dto.getSearchEndDate()).plus(1, ChronoUnit.DAYS)))
//					.collect(Collectors.toList());
//		}
//		if (!StringUtils.isEmpty(dto.getSearchStartAmount())) {
//			list = list.stream().filter(s -> s.getAmount() >= Float.parseFloat(dto.getSearchStartAmount()))
//					.collect(Collectors.toList());
//		}
//		if (!StringUtils.isEmpty(dto.getSearchEndAmount())) {
//			list = list.stream().filter(s -> s.getAmount() <= Float.parseFloat(dto.getSearchEndAmount()))
//					.collect(Collectors.toList());
//		}
//		return ResultUtil.OTSResult(findDisplayDto(list), PTConstants.STATEMENT_DETAILS);
//	}
}
